﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Web;
using System.Web.Mvc;
using SDT.Model;
using SDT.Lib.Paging;
using SDT.Site.ViewModels;
using SDT.Site.Properties;
using System.Threading;
using System.Globalization;

namespace SDT.Site.Controllers
{
    /// <summary>
    /// Author: Takamaru Nguyen
    /// Date: 21/07/2011
    /// </summary>
    public class DocumentController : Controller
    {
        SDTDataContext eDataContext = new SDTDataContext();

        public DocumentController()
        {
            var currentCulture = Thread.CurrentThread.CurrentCulture;
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR");
        }

        //
        // GET: /Document/

        public ActionResult Index()
        {
            DocumentViewModels model = new DocumentViewModels();
            List<DocumentCategory> listC = eDataContext.DocumentCategories.Where(dc => dc.ActiveYN == true).ToList();
            
            foreach (DocumentCategory docC in listC)
            {
                DocumentCategoryItem docCatItem = new DocumentCategoryItem();
                
                docCatItem.Category = docC;
                docCatItem.Documents = eDataContext.FormDocuments
                                        .Where(dc => dc.CatID == docC.ID && dc.ActiveYN == true)
                                        .OrderBy("AddDate desc")
                                        .ToPagedList(0, Settings.Default.DocumentPagSize);

                model.ListDocument.Add(docCatItem);
            }
            model.FileUrl = Settings.Default.AdminRootUrl + Settings.Default.FileRootUrl;

            return View(model);
        }


        /// <summary>
        /// Author: Takamaru Nguyen
        /// Date: 28/07/2011
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Category(int id, int? page)
        {
            DocumentCategoryItem docCatItem = new DocumentCategoryItem();

            page = page.HasValue && page.Value > 0 ? page.Value : 1;
            docCatItem.Category = eDataContext.DocumentCategories.Where(dc => dc.ActiveYN == true && dc.ID == id).FirstOrDefault();
            docCatItem.Documents = eDataContext.FormDocuments
                                    .Where(dc => dc.CatID == id && dc.ActiveYN == true)
                                    .OrderBy("AddDate desc")
                                    .ToPagedList(page.Value - 1, Settings.Default.DocumentPagSize);
            
            docCatItem.DocumentItems = new List<DocumentItem>();
            foreach (FormDocument fd in docCatItem.Documents)
            {
                DocumentItem docItem = new DocumentItem();

                docItem.FormDocument = fd;
                //docItem.Documents = eDataContext.DocumentsInForms
                //                                .Where(d => d.FormDocument.ID == fd.ID && d.Document.ActiveYN == true)
                //                                .Select(d=>d.Document)
                //                                .ToList();
                docItem.Documents = (from doc in eDataContext.Documents
                                     join docIn in eDataContext.DocumentsInForms on doc.ID equals docIn.DocumentID into docInForm
                                     from docu in docInForm.DefaultIfEmpty()
                                     where doc.ActiveYN == true && docu.FormID == fd.ID
                                     select doc).ToList();
                
                docCatItem.DocumentItems.Add(docItem);
            }
            docCatItem.FileRootPath = Settings.Default.AdminRootUrl + Settings.Default.FileRootUrl;

            return View(docCatItem);
        }

        /// <summary>
        /// Author: Takamaru Nguyen
        /// Date: 28/07/2011
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Detail(int id)
        {
            FormDocument fd = eDataContext.FormDocuments.Where(d => d.ID == id && d.ActiveYN == true).FirstOrDefault();
            DocumentItem docItem = new DocumentItem();

            docItem.FormDocument = fd;
            docItem.Documents = (from doc in eDataContext.Documents
                                 join docIn in eDataContext.DocumentsInForms on doc.ID equals docIn.DocumentID into docInForm
                                 from docu in docInForm.DefaultIfEmpty()
                                 where doc.ActiveYN == true && docu.FormID == id
                                 select doc).ToList();

            docItem.FileRootPath = Settings.Default.AdminRootUrl + Settings.Default.FileRootUrl;

            return View(docItem);
        }
    }
}
